package com.fare.taluo.util.flightInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.fare.taluo.application.impl.fetl.SinglePlaneInfoApplicationImpl;
import com.fare.taluo.fetl.domain.entity.SinglePlaneInfoEntity;
import com.fare.taluo.util.DateUtils;
import com.fare.taluo.util.JsonUtil;
import com.fare.taluo.vo.core.CabinEntityVO;
import com.fare.taluo.vo.core.PlaneInfo;
import com.fare.taluo.vo.single.CabinVO;
import com.fare.taluo.vo.single.PlaneInfoJsonVO;

public class SameChannelContrastUtil {
	
	/**
	 * 同渠道走势对比
	 * @param flightDate
	 * @param sourceData
	 * @param map
	 * @return
	 */
	public static Map<String,PlaneInfo> getSingleSpreadForm(List<String> flightDate,List<PlaneInfo> sourceData,Map<String,PlaneInfo> map){
		for (String date : flightDate) {
			for (PlaneInfo planeInfo : sourceData) {
				if(null != planeInfo){
					if(date.equals(planeInfo.getFlightBatchDate())){
						map.put(planeInfo.getCarrierKey()+"_"+date, planeInfo);
					}
				}
				
				
			}
			
		}
		return map;
	}
	/**
	 * 官网走势对比
	 * @param flightDate
	 * @param sourceData
	 * @param map
	 * @return
	 */
	public static Map<String,PlaneInfo> getPlaneToMap(List<String> flightDate,List<PlaneInfo> sourceData,Map<String,PlaneInfo> map){
		for (String date : flightDate) {
			for (PlaneInfo planeInfo : sourceData) {
				if(null != planeInfo){
					if(date.equals(planeInfo.getFlightBatchDate())){
						map.put(planeInfo.getGrabChannelId()+"_"+date, planeInfo);
					}
				}
				
				
			}
			
		}
		return map;
	}
	
	public static List<PlaneInfo> getPlaneInfoList(List<SinglePlaneInfoEntity> entityList){
		 List<PlaneInfo> planes = new ArrayList<PlaneInfo>();
		for (SinglePlaneInfoEntity singlePlaneInfoEntity : entityList) {
			if(null != singlePlaneInfoEntity){
				planes.add(conver2PlaneInfo(singlePlaneInfoEntity));
			}else{
				planes.add(new PlaneInfo());
			}
		}
		return planes;
	}
	
	public static PlaneInfo conver2PlaneInfo(SinglePlaneInfoEntity entity){
		PlaneInfo vo = new PlaneInfo();
		vo.setActuallyFlightNo(entity.getActuallyFlightNo());
		vo.setAgentName(entity.getAgentName());
		vo.setAreaCode(entity.getAreaCode());
		vo.setAreaName(entity.getAgentName());
		vo.setCarrierFullName(entity.getCarrierFullName());
		vo.setCarrierKey(entity.getCarrierKey());
		vo.setCarrierName(entity.getCarrierName());
		vo.setLowerPrice(""+entity.getTotalLowestPrice());
		vo.setHighPrice(""+entity.getTotalHighestPrice());
		vo.setFlightId(entity.getFlightNo());
		vo.setFromCity(entity.getFromCity());
		vo.setFromCityName(entity.getFromCityName());
		vo.setGrabChannelId(""+entity.getAttachHbaseKey());
		vo.setGrabChannel(entity.getGrabChannelName());
		vo.setSOURCE_ROWKEY(entity.getSourceRowkey());
		vo.setFlightBatchStartTime(DateUtils.formatDay(entity.getStartTime(), "yyyyMMddHHmmss"));
		vo.setFlightBatchEndTime(DateUtils.formatDay(entity.getEndTime(), "yyyyMMddHHmmss"));
		vo.setToCity(entity.getToCity());
		vo.setToCityName(entity.getToCityName());
		vo.setFlightBatchDate(entity.getFlightDate());
		vo.setGrapDate(entity.getCreateTime());
		vo.setGrabDate(DateUtils.formatDay(entity.getCreateTime(), "yyyyMMddHHmmssSSS"));
		return vo;
				
	}
	
	public static PlaneInfo  conver2Planeinfo(SinglePlaneInfoEntity entity,String cabinType,String priceType){
		SinglePlaneInfoApplicationImpl application =new	SinglePlaneInfoApplicationImpl();
		PlaneInfo vo = new PlaneInfo();
		vo.setActuallyFlightNo(entity.getActuallyFlightNo());
		vo.setAgentName(entity.getAgentName());
		vo.setAreaCode(entity.getAreaCode());
		vo.setAreaName(entity.getAgentName());
		vo.setCarrierFullName(entity.getCarrierFullName());
		vo.setCarrierKey(entity.getCarrierKey());
		vo.setCarrierName(entity.getCarrierName());
		//vo.setCreateTime(entity.getCreateTime());
		//vo.setEndTime(entity.getEndTime());
		//vo.setFlightDate(entity.getFlightDate());
		vo.setFlightId(entity.getFlightNo());
		vo.setFromCity(entity.getFromCity());
		vo.setFromCityName(entity.getFromCityName());
		vo.setGrabChannelId(""+entity.getAttachHbaseKey());
		vo.setGrabChannel(entity.getGrabChannelName());
		vo.setSOURCE_ROWKEY(entity.getSourceRowkey());
		vo.setFlightBatchStartTime(DateUtils.formatDay(entity.getStartTime(), "yyyyMMddHHmmss"));
		vo.setFlightBatchEndTime(DateUtils.formatDay(entity.getEndTime(), "yyyyMMddHHmmss"));
		vo.setToCity(entity.getToCity());
		vo.setToCityName(entity.getToCityName());
		vo.setFlightBatchDate(entity.getFlightDate());
		vo.setGrapDate(entity.getCreateTime());
		vo.setGrabDate(DateUtils.formatDay(entity.getCreateTime(), "yyyy-MM-dd hh:mm:ss"));
		//vo.setTotalHighestPrice(entity.getTotalHighestPrice());
		//svo.setTotalLowestPrice(entity.getTotalLowestPrice());
		if(null!=entity.getPlaneInfoJsonEntity()){
			PlaneInfoJsonVO vojson = application.planeInfoJsonEntityToVO(entity.getPlaneInfoJsonEntity());
			if(null!=vojson.getCabinsJson()){
				Set<CabinVO> setCabins =new HashSet<CabinVO>(JsonUtil.getDTOList(vojson.getCabinsJson(), CabinVO.class));
				Double tmpMax = Double.MAX_VALUE;
				Double tmpMin = Double.MIN_VALUE;
				Double tmp = 0.0;
				CabinEntityVO voToAdd = new CabinEntityVO();
				//根据价格类型和舱位类型找出 该舱位类型下的最高价舱位或最低价舱位
				for (CabinVO cabinVO : setCabins) {
					Double price = cabinVO.getPrice();
					if(cabinType.contains(cabinVO.getCabinType()) || cabinVO.getCabinType().contains(cabinType)){
						//最低价
						if("lowest".equalsIgnoreCase(priceType) && price <= tmpMax){
							tmpMax = price ;
							tmp = price;
						}else if("highest".equalsIgnoreCase(priceType) && price >= tmpMin){
							tmpMin = price ;
							tmp = price;
						}
					}
					
				}
				voToAdd.setCabinType(cabinType);
				voToAdd.setPrice(tmp);
				vo.getCabins().add(voToAdd);
			}
		}
	
		
		return vo;
		
	}
	public static String getSpreadToString(String lowwerPrice, String nativePrice){
		if(!"0.0".equals(nativePrice)){
			Double spread = (Double.parseDouble(lowwerPrice) - Double.parseDouble(nativePrice)) / Double.parseDouble(nativePrice);
			return String.format("%.2f",spread*100)+"%";
		}else {
			return null;
		}
	
	}
	
	public static List<SinglePlaneInfoEntity> timeFilter(String[] timeArr , List<SinglePlaneInfoEntity> tempEntityList){
		//String time = vo.getCheckbox();
		Map<String,String> map = new HashMap<String, String>();
		List<String> list  = new ArrayList<String>();
		list.add("M-8-12");
		list.add("B-12-14");
		list.add("A-14-18");
		list.add("N-18-24");
	//	String[] timeArr = time.split(",");
		for (String string : timeArr) {
			if(string.equals("M")){
				map.put("M-8-12", "8-12");
			}else if(string.equals("B")){
				map.put("B-12-14", "12-14");
			}else if(string.equals("A")){
				map.put("A-14-18", "14-18");
			}else if(string.equals("N")){
				map.put("N-18-24", "18-24");
			}
			
		}
		for (String string : list) {
			if(!map.containsKey(string)){
				getListAfterFilter(string,tempEntityList);
			}
		}
		return tempEntityList;
	}
	private static List<SinglePlaneInfoEntity> getListAfterFilter(String time,List<SinglePlaneInfoEntity> listlist){
		int start = Integer.parseInt(time.split("-")[1]);
		int end = Integer.parseInt(time.split("-")[2]);
				for (Iterator it = listlist.iterator(); it.hasNext(); ) {   
					SinglePlaneInfoEntity plane = (SinglePlaneInfoEntity) it.next();
					   //int flightTime = Integer.parseInt(DateUtils.formatDay(plane.getStartTime().).substring(0, 2));
					   int flightTime = plane.getStartTime().getHours();
					   
					   if((flightTime>= 0 && flightTime <8 && end == 24)){//0-8也是晚上
						   
						   it.remove();   
					   }
					   if (flightTime<=end && flightTime>=start) {   
					       it.remove();   
					   }   
					}  
		return listlist;
	}
	public static List<SinglePlaneInfoEntity> getLowestByLine(List<SinglePlaneInfoEntity> tempEntityList){
		double min = Double.MAX_VALUE;
		List<SinglePlaneInfoEntity> returnList = new ArrayList<SinglePlaneInfoEntity>();
		SinglePlaneInfoEntity entityTemp = null;
		for (SinglePlaneInfoEntity singlePlaneInfoEntity : tempEntityList) {
			if(null != singlePlaneInfoEntity && null != singlePlaneInfoEntity.getTotalLowestPrice()){
				if(singlePlaneInfoEntity.getTotalLowestPrice() < min){
					min = singlePlaneInfoEntity.getTotalLowestPrice();
					entityTemp = singlePlaneInfoEntity;
				}
			}
		}
		if(entityTemp != null){
			returnList.add(entityTemp);
		}
		return returnList;
	}
	/**
	 * 找到该航线下的每天的最低价航班
	 * @param tempEntityList
	 * @return
	 */
	public static List<SinglePlaneInfoEntity> getLowestByLineEachDay(List<SinglePlaneInfoEntity> tempEntityList,List<String> dateList){
		
		List<SinglePlaneInfoEntity> returnList = new ArrayList<SinglePlaneInfoEntity>();	
		for (String date : dateList) {
			double min = Double.MAX_VALUE;
			SinglePlaneInfoEntity entityTemp = null;
			for (SinglePlaneInfoEntity singlePlaneInfoEntity : tempEntityList) {
				if(null != singlePlaneInfoEntity && null != singlePlaneInfoEntity.getTotalLowestPrice()){
					if(singlePlaneInfoEntity.getTotalLowestPrice() < min && date.equals(singlePlaneInfoEntity.getFlightDate())){
						min = singlePlaneInfoEntity.getTotalLowestPrice();
						entityTemp = singlePlaneInfoEntity;
					}
				}
			}
			if(entityTemp != null){
				returnList.add(entityTemp);
			}
		
		}
		return returnList;
	}
	/**
	 * 找到该航线下的每天的最高价航班
	 * @param tempEntityList
	 * @return
	 */
	public static List<SinglePlaneInfoEntity> getHighestByLineEachDay(List<SinglePlaneInfoEntity> tempEntityList,List<String> dateList){
		
		List<SinglePlaneInfoEntity> returnList = new ArrayList<SinglePlaneInfoEntity>();
		for (String date : dateList) {
			double max = Double.MIN_VALUE;
			SinglePlaneInfoEntity entityTemp = null;

			for (SinglePlaneInfoEntity singlePlaneInfoEntity : tempEntityList) {
				if(null != singlePlaneInfoEntity && null != singlePlaneInfoEntity.getTotalHighestPrice()){
					if(singlePlaneInfoEntity.getTotalHighestPrice() > max && date.equals(singlePlaneInfoEntity.getFlightDate())){
						max = singlePlaneInfoEntity.getTotalHighestPrice();
						entityTemp = singlePlaneInfoEntity;
					}
				}
			}
			if(entityTemp != null){
				returnList.add(entityTemp);
			}
		}
		return returnList;
	}
	public static List<SinglePlaneInfoEntity> getHighestByLine(List<SinglePlaneInfoEntity> tempEntityList){
		double max = Double.MIN_VALUE;
		List<SinglePlaneInfoEntity> returnList = new ArrayList<SinglePlaneInfoEntity>();
		SinglePlaneInfoEntity entityTemp = null;
		for (SinglePlaneInfoEntity singlePlaneInfoEntity : tempEntityList) {
			if(null != singlePlaneInfoEntity && null != singlePlaneInfoEntity.getTotalHighestPrice()){
				if(singlePlaneInfoEntity.getTotalHighestPrice() > max){
					max = singlePlaneInfoEntity.getTotalHighestPrice();
					entityTemp = singlePlaneInfoEntity;
				}
			}
		}
		if(entityTemp != null){
			returnList.add(entityTemp);
		}
		return returnList;
	}
	/**
	 * 对查询出来的数据按日期过滤
	 * @param inputList
	 * @param dateArray
	 * @return
	 */
	public static List<SinglePlaneInfoEntity> filterByDate(List<SinglePlaneInfoEntity> inputList,List<String> dateArray){
		List<SinglePlaneInfoEntity> returnList = new ArrayList<SinglePlaneInfoEntity>();
		for (String dateString : dateArray) {
			for (SinglePlaneInfoEntity singlePlaneInfoEntity : inputList) {
				if(null != singlePlaneInfoEntity && dateString.equalsIgnoreCase(singlePlaneInfoEntity.getFlightDate())){
					returnList.add(singlePlaneInfoEntity);
				}
				
			}
		}
		return returnList;
	}
}